Your First Program

Objectives


Discussion

Visual Basic (VB)

Visual Studio (VS)

Displaying Information

messagebox.show("Your message here")

console.writeline("Your message here")

Back to top


Demonstration

In this demonstration we will show how to write a VB program.  

1.  Start Visual Studio (VS).  When VS opens, a start page will appear. You can create a new project (or program) by selecting the New Project button.  You can also use the menu to create a new project by selecting File...New...Project.  After you have chosen to create a new project, the following window will appear.

2.  Notice that there are many different types of projects that you can create.  Select the following:

3.  Enter the name for your project.  This name will be used for some of the files in the project and for the folder that all of your project files will be stored in.  Select the location for your project.  Click OK and your new project will be created.

4.  The Integrated Design Environment (IDE) is the fancy name for the VS interface.   An image of the IDE is shown below.  In the center of the IDE is the form designer and code editor window.  Currently a form named Form1 is displayed.  This is the form that will appear when you run your program.  On the left side of the IDE is the Toolbox.  If the Toolbox is not visible select View...Toolbox in the menu.   The Toolbox contains controls or objects which we can add to our form.  On the right side of the screen should be the properties window. If it is not present then click View...Properties in the menu.  It displays the properties for an object.

5.  Notice the buttons near the top of the properties window.  Click on these to determine what they do.  Notice that you can change how the properties in the window are arranged.

IDE

6.  Move the cursor over the various buttons in the toolbar to see what each does.   When you hold the cursor over a button a tool tip will show what the button does.  Find the Start button.  Even though you haven't programmed anything yet, run the program by selecting the Start  button or clicking Debug...Start in the menu.  The form will appear. Stop your program by clicking the close button in the upper right corner of the form or click the Stop Debugging  button or select Debug...Stop Debugging in the menu.

7.  Now you will check out the code that VS has already created for you.  Double click on the form.  When you do this, the code window for the form will open.  Click on the "+" sign next to the expression "Windows Form Designer Generated Code."  Notice all of the code that VS has generated.  Don't worry about the details at this point but understand that VS has done a lot of work for you.

8.  VB programs consist of two primary parts: the graphical user interface (GUI) which is what the user sees, and the code which makes the GUI function. Generally when we write a program, we design the GUI first and then write the code to make the GUI work. We are going to start our program by creating the GUI.  First we will add a button to our form.  A button is a control (or object) that is located in the Toolbox.  You can add a button several ways: (1) by double clicking on it in the toolbox, (2) by dragging it from the toolbox and dropping it on the form, or (3) by writing code.  We will discuss #3 in a later lesson.   Notice that you can move the button around the form and resize it by dragging one of its edges.

9.  Once we have added a button to the form we can make the button do something.  However, first we are going to set some of the button's properties.  A button is an Object and it has properties, methods, and events.  Properties are just characteristics of an object.  Click the button to make it the selected item. Notice that the Properties window displays the properties for the button.  Change the name property of the button to "btnHello".  Although we really don't have to change the name of the control we should always give it a relevant name and we should always change it immediately.  Change the text property to "Hello".  Change the backcolor property to your favorite color.  Notice how easy it is to set a control's properties.

10.  We are now going to write the code to make the button work.  Double click on the button that is on your form.  Make sure that you double-click on the button and not the form.  When you double-click a control, VS opens the code window and displays the code for the "most common" event-handler for that control.  Remember that a control is an object and has properties, methods, and events.  An event is just something that a control responds to.  For example, when you run the program, the user can "click" on a button.  Therefore, one event for a button is a Click event.  Now let's write the code so that the button can respond to the click event.  Type the following highlighted code in the btnHello_Click event subroutine.

Private Sub btnHello_Click(ByVal sender As System.Object, ...
Console.Write("Hello World!")
End Sub

11.  Now run your program and click the button.  To see the results, you might have to open the output window. If the Output window is not visible, select View...OtherWindows...Output.  Now you should see "Hello World" displayed in the output window.  This is probably the most simple and most boring program that you will ever write.  But be patient, the best is yet to come.

12. Let's make it a little more interesting.  Replace the highlighted code above with:

MessageBox.Show("Hello World")

Notice that you are not able to edit the code if you did not stop running your program.

13.  Run the program, click the button and a message box should appear with your message in it.  Depending on your perspective, you may be thinking that this is really cool or this is really no big deal.  If you have programmed in some other languages then you may be really excited to think that you only typed in one line of code to make this program work.  In some languages, you would write pages of code to create a window and make the message box appear.

14.  Save your project by clicking the Save icon in the toolbar or File...SaveAll in the menu.  Remember to save your projects frequently.  Close your project.  Close VS.

15. Start VS again and open the project that you just created.  Notice that there are several ways to open a project.  You can select the project from the recently-opened projects that appear on the start page or you can click the button Open Project or you can choose File...Open...Project from the menu.  Try each of these methods so that you are comfortable with finding and opening projects that you have already created.

Back to top


Exercises

1.  Create a new project and write a program that meets the following requirements:

  1. It has a button with your name on it.
  2. When the user clicks the button, a messagebox appears with a message "Welcome to my world" in it.
  3. The button is red or some other funky color.
  4. The button is centered on the form.
  5. The title on the form says anything except "Form1."
  6. The color of the form is yellow or some other funky color.

When you run your program you may find the color scheme to be irritating.  The purpose of this exercise was to get you to practice writing programs and become familiar with the Properties Window.  If you are writing a program for others to use, you need to be very careful about your color selection.  We recommend using the standard colors unless you have a strong reason to change them. 

2.  Modify your program created in #1 so that a second messagebox appears after the first.  The second messagebox should display the message "Thanks for coming!"

3.  Modify your program created in #1 so that the button is disabled (the user won't be able to click on it).

4.  Modify your program created in #1 so that the button is not visible when the program runs.

5.  Explain the difference between Visual Studio and Visual Basic.

6.  How many different types of projects can you create?

Back to top


Links & Help

1. In the menu, select Help...Search.  In the Search Window set the following:
    Look for :  "Introduction to windows forms"
    Filtered by : "Visual Basic"
    Select Search in titles only
In the Search Results window select Introduction to  Windows Forms.

2. Repeat the search in #1 except set Look for to "Customizing the Development Environment"

3. Repeat the search in #1 except set Look for to "Windows Forms Designer"

4.  Repeat the search in #1 except set Look for to "Adding Controls to Windows Forms"

Back to top